package com.example.administrator.footballgamecollectandanalyze;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import tool.MyApplication;

public class LoginActivity extends AppCompatActivity {

    private Button btnLogin;
    private Button btnRegister;
    TextView tvUserName;
    TextView tvPassword;
    private MyApplication app;

    String url = "http://112.74.26.238:8080/football/login";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        tvUserName = (TextView) findViewById(R.id.userName);
        tvPassword = (TextView) findViewById(R.id.password);
        btnLogin = (Button) findViewById(R.id.sign_in_button);
        btnRegister = (Button) findViewById(R.id.Register_button);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = tvUserName.getText().toString();// 用户名
                String userPass = tvPassword.getText().toString();// 用户密码
                //判断用户名和密码是否为空
                if (TextUtils.isEmpty(userName.trim())
                        || TextUtils.isEmpty(userPass.trim())) {
                    Toast.makeText(LoginActivity.this, "用户名或者密码不能为空", Toast.LENGTH_LONG).show();
                } else {
                    // 发送请求给服务器
                    //调用：loginByAsyncHttpClientPost(userName, userPass);
                    loginByAsyncHttpClientPost(userName, userPass);
                }
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }

    public void loginByAsyncHttpClientPost(final String userName, String userPass) {
        AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
        // 创建请求参数的封装的对象
        RequestParams params = new RequestParams();
        params.put("username", userName); // 设置请求的参数名和参数值
        params.put("password", userPass);// 设置请求的参数名和参数

        // 执行post方法
        RequestHandle post = client.get(url, params, new AsyncHttpResponseHandler() {
            /**
             * 成功处理的方法
             * statusCode:响应的状态码; headers:相应的头信息 比如 响应的时间，响应的服务器 ;
             * responseBody:响应内容的字节
             */
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  byte[] responseBody) {
                if (statusCode == 200) {
                    String str = new String(responseBody);
                    try {
                        JSONObject jsonObject = new JSONObject(str);
                        String code = jsonObject.getString("code");
                        if (code.equals("0")) {
                            app = (MyApplication) getApplication();
                            app.setMana_id(userName);
//                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        } else {
                            Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }

            /**
             * 失败处理的方法
             * error：响应失败的错误信息封装到这个异常对象中
             */
            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  byte[] responseBody, Throwable error) {
                error.printStackTrace();// 把错误信息打印出轨迹来
                //Toast.makeText(MyApplication.getContext(), "网络错误", Toast.LENGTH_LONG).show();
            }
        });
    }

}


